<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自己来</title>
</head>
<body>
    <script>
        class MyPromise{
            static PENDING = 'pending'
            static FULFILLED = 'fulfilled'
            static REJECTED = 'rejected'
            constructor(promiseCbk) {
                this.status = MyPromise.PENDING;
                this.value = null;
                this.resultTasks = []
                try {
                    promiseCbk(this.resolve.bind(this), this.reject.bind(this))
                } catch (error) {
                    this.reject(error)
                }
            }
            resolve(value) {
                if (this.status === MyPromise.PENDING) {
                    this.status = MyPromise.FULFILLED;
                    this.value = value;
                    this.resultTasks.forEach(item => {
                        item.onFulfilled(value)
                    })
                }
                
            }
            reject(errMsg) {
                if (this.status === MyPromise.PENDING) {
                    this.status = MyPromise.REJECTED;
                    this.value = errMsg;
                    this.resultTasks.forEach(item => {
                        item.onRejected(errMsg)
                    })
                }
            }
            then(onFulfilled, onRejected) {
                if (typeof onFulfilled !== 'function') {
                    onFulfilled = () => this.value
                }
                if (typeof onRejected !== 'function') {
                    onRejected = () => this.value
                }
                return new MyPromise((resolve, reject)=> {
                    if (this.status === MyPromise.PENDING) {
                        this.resultTasks.push({
                            onFulfilled: (value) => {
                                try {
                                    let res = onFulfilled(value)
                                    resolve(res)
                                } catch (error) {
                                    onRejected(error)
                                }
                            },
                            onRejected: (error) => {
                                try {
                                    let res = onRejected(error)
                                    resolve(res)
                                } catch (error) {
                                    onRejected(error)
                                }
                            }
                        })
                    }
                    if (this.status === MyPromise.FULFILLED) {
                        try {
                            let res = onFulfilled(this.value)
                            resolve(res)
                        } catch (error) {
                            onRejected(error)
                        }
                        
                    }
                    if (this.status === MyPromise.REJECTED) {
                        try {
                           let res = onRejected(this.value)
                            resolve(res)
                        } catch (error) {
                            onRejected(error)
                        }
                        
                    }
                })
            }
        }


        let res = new MyPromise((resolve, reject) => {
            // reject(2);
            // console.log(asdas)
            // setTimeout(() => {
                reject(1);
            // }, 1000);
            
        }).then(res => {
            return 88
        }, err => {
            console.log('MyPromise->reject:', err)
            return 77
        }).then().then(res => {
            console.log('aaa', res)
        })
        // console.log(res, 'res')
    </script>
</body>
</html>